package com.ymatou.iapi.smsproxy.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.smsproxy.parameter.SendMessageBean;
import com.ymatou.iapi.smsproxy.parameter.SendSmsVerifyCodeBean;
import com.ymatou.iapi.smsproxy.service.SendMessageCall;
import com.ymatou.iapi.smsproxy.service.SendSmsVerifyCodeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.TestDataManager;
/**
*发送短信模板
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendSmsVerifyCode{
	private static SendSmsVerifyCodeBean sendsmsverifycodeBean;
	private static SendSmsVerifyCodeCall sendsmsverifycodeCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送短信模板");
	}

	@Before
	public void caseUp() {
		sendsmsverifycodeBean=new SendSmsVerifyCodeBean();
		sendsmsverifycodeCall=new SendSmsVerifyCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendsmsverifycodeCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(SendSmsVerifyCodeBean sendm,SendSmsVerifyCodeCall sendmessageCall) throws Exception
	{
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		
		List<String> phones=new ArrayList<String>();
		phones.add(sendm.getPhone());
		String content="IsCall:false , VerifyCode:"+sendm.getCode()+" , Language:zh-cn , MessageTemplate:验证码为：$$CODE$$";
		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(sendm.getAppId(), content, phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		Logger.debug(com.ymt.core.tool.FormatUtil.beanToGSONString(smsRecord));
		//{"_id":"201712-654f723a33314e5995bd7826b35df127","AppId":"app-java-teleSign","ApiName":"SendMessage","ChannelId":"java-teleSign","ChannelName":"teleSign","Phones":[SendSmsCallService.TESTPHONE],"Content":"autotest_发送短信_6106515124","CreatedTime":{"$date":"2017-12-05T02:44:30.424Z"},"SendedTime":{"$date":"2017-12-05T02:44:32.360Z"},"ClientIp":"172.16.101.12:61584","ServerIp":"172.16.101.112","MessageAction":6,"Comment":"java app","ResponseMessage":"{\"resource_uri\": \"/v1/verify/2580923D69A8070490409157579C048A\", \"reference_id\": \"2580923D69A8070490409157579C048A\", \"sub_resource\": \"sms\", \"errors\": [], \"verify\": {\"code_state\": \"UNKNOWN\", \"code_entered\": \"\"}, \"status\": {\"updated_on\": \"2017-12-05T02:44:22.871057Z\", \"code\": 290, \"description\": \"Message in progress\"}}","Number":1}
		Logger.verifyEquals(sendm.getAppId(), smsRecord.getAppId(), "验证appid");
		Logger.verifyIsContains(sendm.getPhone(), smsRecord.getPhones().toString(), "验证phone");
		Logger.verifyEquals(content, smsRecord.getContent(), "验证content");
		Logger.verifyEquals("SendSmsVerifyCode", smsRecord.getApiName(), "验证ApiName");
		if(sendm.getAppId().equals("app-java-teleSign")){
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("testSmsDriver", smsRecord.getChannelName(), "验证ChannelName");
		}else if(sendm.getAppId().equals("app-java-mw")){
			Logger.verifyEquals("java-mw", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("mw", smsRecord.getChannelName(), "验证ChannelName");
		}else if(sendm.getAppId().equals("app-java-ctc")){
			Logger.verifyEquals("java-ctc", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("ctc", smsRecord.getChannelName(), "验证ChannelName");
		}else if(sendm.getAppId().equals("app-java-qq")){
			Logger.verifyEquals("java-qq", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
		}else if(sendm.getAppId().equals("app-java-qq-i")){
			Logger.verifyEquals("java-qq-i", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("qq", smsRecord.getChannelName(), "验证ChannelName");
		}else if(sendm.getAppId().equals("app-java-dl")){
			Logger.verifyEquals("java-dl", smsRecord.getChannelId(), "验证ChannelId");
			Logger.verifyEquals("dl", smsRecord.getChannelName(), "验证ChannelName");
		}
		Logger.verifyNotNull(smsRecord.getCreatedTime(), "验证createdtime");
		Logger.verifyNotNull(smsRecord.getSendedTime(), "验证SendedTime");
		Logger.verifyNotNull(smsRecord.getClientIp(), "验证ClientIp");
		Logger.verifyNotNull(smsRecord.getServerIp(), "验证ServerIp");
		Logger.verifyEquals(1, smsRecord.getNumber(), "验证Number");
		//TODO MessageAction 6成功 7失败 8 黑名单
//		Logger.verifyEquals("java app", smsRecord.getComment(), "验证Comment");
		Logger.verifyIsContains(smsRecord.get_id(),sendmessageCall.getString("result"), "验证result的id数据一致");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendSmsVerifyCode_001() {
		Logger.start(true,"发送短信模板app-java-teleSign成功");
		try {
			sendsmsverifycodeBean.setAppId("app-java-teleSign");
			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendsmsverifycodeBean.setLanguage("zh-cn");
			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyEquals("success",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("0",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("true",sendsmsverifycodeCall.getString("success"),"验证success");
			verifyDB(sendsmsverifycodeBean,sendsmsverifycodeCall);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendSmsVerifyCode_002() {
		Logger.start(true,"不输入phone失败");
		try {
			sendsmsverifycodeBean.setAppId("app-java-teleSign");
			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendsmsverifycodeBean.setLanguage("zh-cn");
//			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyIsContains("phone",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("2",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("false",sendsmsverifycodeCall.getString("success"),"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendSmsVerifyCode_003() {
		Logger.start(true,"不输入appid失败");
		try {
//			sendsmsverifycodeBean.setAppId("app-java-teleSign");
			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendsmsverifycodeBean.setLanguage("zh-cn");
			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyIsContains("应用编号不能为空",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("1",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("false",sendsmsverifycodeCall.getString("success"),"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendSmsVerifyCode_004() {
		Logger.start(true,"不输入code失败");
		try {
			sendsmsverifycodeBean.setAppId("app-java-teleSign");
//			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendsmsverifycodeBean.setLanguage("zh-cn");
			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyIsContains("验证码不能为空",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("1",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("false",sendsmsverifycodeCall.getString("success"),"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	@Ignore
	public void Tc_SendSmsVerifyCode_005() {
		Logger.start(true,"不输入language默认成功");
		try {
			sendsmsverifycodeBean.setAppId("app-java-teleSign");
			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
//			sendsmsverifycodeBean.setLanguage("zh-cn");
			//影响Language:zh-cn  String content="IsCall:false , VerifyCode:"+sendm.getCode()+" , Language:zh-cn , MessageTemplate:验证码为：$$CODE$$";
			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyEquals("success",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("0",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("true",sendsmsverifycodeCall.getString("success"),"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_SendSmsVerifyCode_006() {
		Logger.start(true,"不输入messageTemplate失败");
		try {
			sendsmsverifycodeBean.setAppId("app-java-teleSign");
			sendsmsverifycodeBean.setCode(TestDataManager.getRandomNum(4));
			sendsmsverifycodeBean.setLanguage("zh-cn");
			sendsmsverifycodeBean.setPhone(SendSmsCallService.TESTPHONE);
//			sendsmsverifycodeBean.setMessageTemplate("验证码为：$$CODE$$");
			sendsmsverifycodeCall.setData(sendsmsverifycodeBean);
			sendsmsverifycodeCall.callService();			
			Logger.verifyIsContains("messageTemplate",sendsmsverifycodeCall.getString("errorMessage"),"验证errorMessage");
			Logger.verifyEquals("2",sendsmsverifycodeCall.getString("errorCode"),"验证errorCode");
			Logger.verifyEquals("false",sendsmsverifycodeCall.getString("success"),"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}